using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;

public partial class GonzalesAdminEntityDetail : System.Web.UI.Page
{

    #region Variables

    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;

    #region EntityID
    protected string EntityID
    {
        get
        {
            return ViewState["EntityID"].ToString();
        }

        set
        {
            ViewState["EntityID"] = value;
        }
    }
    #endregion

    #endregion

    #region Events

    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();

        if (!IsPostBack)
        {
            this.SwitchLanguage();
            this.PageComponentSetup();
            this.SwitchLanguage();

            this.LoadData();
        }
    }
    protected void Button_Submit_Click(object sender, EventArgs e)
    {
        if (this.DataValidate())
        {
            using (EntityManager CurEntityManager = new EntityManager(this.CurrentPrivateSession))
            {
                Entity CurEntity = new Entity(this.EntityID, this.TextBox_EntityName.Text);
                CurEntity.BPCS = this.TextBox_ADBPCS.Text;
                CurEntity.Order = Int32.Parse(this.TextBox_Order.Text);                

                if (CurEntityManager.UpdateEntity(CurEntity))
                {
                    Response.Redirect("GonzalesAdminEntityListing.aspx");
                }
                else
                {
                    this.ShowErrorMessage("Data updated failed.");
                    this.CurLogManager.LogError("[GonzalesAdminEntityDetail.Button_Submit_Click] : Failed at CurEntityManager.UpdateEntity() : " + CurEntityManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                }
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("GonzalesAdminEntityListing.aspx");
    }

    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {
       this.ImageButton_Cancel.Attributes["onclick"] = "return confirm('All unsaved data will be lost, are you sure?');";
    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                /*
                this.Label_Information.Text = CurLanguageFactory.DisplayValue("Information");
                this.Label_PageTitle.Text = CurLanguageFactory.DisplayValue("PageTitle");
                this.Label_Remarks.Text = CurLanguageFactory.DisplayValue("Remarks");

                this.Item_DateCreated = CurLanguageFactory.DisplayValue("ItemDateCreated");
                this.Item_ClaimID = CurLanguageFactory.DisplayValue("ItemClaimID");
                this.Item_Purpose = CurLanguageFactory.DisplayValue("ItemPurpose");
                this.Item_Currency = CurLanguageFactory.DisplayValue("ItemCurrency");
                this.Item_Amount = CurLanguageFactory.DisplayValue("ItemAmount");
                this.Item_Status = CurLanguageFactory.DisplayValue("ItemStatus");
                this.Item_PaymentDate = CurLanguageFactory.DisplayValue("ItemPaymentDate");

                this.Status_All = CurLanguageFactory.DisplayValue("StatusAll");
                this.Status_Submitted = CurLanguageFactory.DisplayValue("StatusSubmitted");
                this.Status_Approved = CurLanguageFactory.DisplayValue("StatusApproved");
                this.Status_Escalated = CurLanguageFactory.DisplayValue("StatusEscalated");
                this.Status_Processed = CurLanguageFactory.DisplayValue("StatusProcessed");
                this.Status_Paid = CurLanguageFactory.DisplayValue("StatusPaid");
                this.Status_RejectedByApprover = CurLanguageFactory.DisplayValue("StatusRejectedByApprover");
                this.Status_RejectedByFinance = CurLanguageFactory.DisplayValue("StatusRejectedByFinance");

                this.SortByASC = CurLanguageFactory.DisplayValue("SortOrderASC");
                this.SortByDESC = CurLanguageFactory.DisplayValue("SortOrderDESC");
                */

            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesAdminAuthorizedDetail.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #region ShowErrorMessage

    protected void ShowErrorMessage(string ErrorMessage)
    {
        this.Table_MessageBox.Visible = true;
        this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessage;
    }

    protected void ShowErrorMessage(ArrayList ErrorMessageList)
    {
        if (ErrorMessageList != null)
        {
            StringBuilder ErrorMessageBuilder = new StringBuilder("<ul>");

            for (int x = 0; x < ErrorMessageList.Count; x++)
            {
                ErrorMessageBuilder.AppendLine("<li>" + ErrorMessageList[x] + "</li>");
            }

            ErrorMessageBuilder.AppendLine("</ul>");

            this.Table_MessageBox.Visible = true;
            this.Table_MessageBox.Rows[0].Cells[0].Text = ErrorMessageBuilder.ToString();
        }
    }

    #endregion

    #endregion

    #region Data Operation

    #region LoadData
    protected void LoadData()
    {
        if (Request.QueryString["id"] != null)
        {
            this.EntityID = Request.QueryString["id"];            

            // setup detail
            using (EntityManager CurEntityManager = new EntityManager(this.CurrentPrivateSession))
            {
                Entity CurEntity = CurEntityManager.GetEntityDetailByCode(this.EntityID);

                this.LabelValue_EntityID.Text = CurEntity.InternalID;
                this.TextBox_ADBPCS.Text = CurEntity.BPCS;
                this.TextBox_EntityName.Text = CurEntity.DisplayName;
                this.TextBox_Order.Text = CurEntity.Order.ToString();                
            }
        }
        else
        {
            this.ShowErrorMessage("Should follow actual path");
        }
    }

    #endregion

    #region DataValidate
    protected bool DataValidate()
    {
        bool smooth = true;

               

        return smooth;
    }
    #endregion

    #endregion
}
